package org.htmlparser.util.sort;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class Sort
{
  public static void QuickSort(Vector paramVector)
    throws ClassCastException
  {
    QuickSort(paramVector, 0, paramVector.size() - 1);
  }

  public static void QuickSort(Vector paramVector, int paramInt1, int paramInt2)
    throws ClassCastException
  {
    Ordered localOrdered;
    int k;
    int i = paramInt1;
    int j = paramInt2;
    if (paramInt2 > paramInt1)
    {
      localOrdered = (Ordered)paramVector.elementAt((paramInt1 + paramInt2) / 2);
      if (i <= j)
        for (k = i; (k < paramInt2) && (((Ordered)paramVector.elementAt(k)).compare(localOrdered) < 0); ++k);
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while ((l > paramInt1) && (((Ordered)paramVector.elementAt(l)).compare(localOrdered) > 0))
            --l;
          if (k > l)
            break label149;
          i = k + 1;
          j = l - 1;
          swap(paramVector, k, l);
        }
        if (paramInt1 < j)
          QuickSort(paramVector, paramInt1, j);
        if (i < paramInt2)
          QuickSort(paramVector, i, paramInt2);
        return;
        label149: j = l;
        i = k;
      }
      int l = j;
    }
  }

  public static void QuickSort(Sortable paramSortable)
  {
    QuickSort(paramSortable, paramSortable.first(), paramSortable.last());
  }

  public static void QuickSort(Sortable paramSortable, int paramInt1, int paramInt2)
  {
    Ordered localOrdered1;
    Ordered localOrdered2;
    int k;
    int i = paramInt1;
    int j = paramInt2;
    if (paramInt2 > paramInt1)
    {
      localOrdered1 = paramSortable.fetch((paramInt1 + paramInt2) / 2, null);
      localOrdered2 = null;
      if (i <= j)
        for (k = i; k < paramInt2; ++k)
        {
          localOrdered2 = paramSortable.fetch(k, localOrdered2);
          if (localOrdered2.compare(localOrdered1) >= 0)
            break;
        }
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (l > paramInt1)
          {
            localOrdered2 = paramSortable.fetch(l, localOrdered2);
            if (localOrdered2.compare(localOrdered1) <= 0)
              break;
            --l;
          }
          if (k > l)
            break label164;
          i = k + 1;
          j = l - 1;
          paramSortable.swap(k, l);
        }
        if (paramInt1 < j)
          QuickSort(paramSortable, paramInt1, j);
        if (i < paramInt2)
          QuickSort(paramSortable, i, paramInt2);
        return;
        label164: j = l;
        i = k;
      }
      int l = j;
    }
  }

  public static void QuickSort(String[] paramArrayOfString)
  {
    QuickSort(paramArrayOfString, 0, paramArrayOfString.length - 1);
  }

  public static void QuickSort(String[] paramArrayOfString, int paramInt1, int paramInt2)
  {
    String str;
    int k;
    int i = paramInt1;
    int j = paramInt2;
    if (paramInt2 > paramInt1)
    {
      str = paramArrayOfString[((paramInt1 + paramInt2) / 2)];
      if (i <= j)
        for (k = i; (k < paramInt2) && (paramArrayOfString[k].compareTo(str) < 0); ++k);
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while ((l > paramInt1) && (paramArrayOfString[l].compareTo(str) > 0))
            --l;
          if (k > l)
            break label130;
          i = k + 1;
          j = l - 1;
          swap(paramArrayOfString, k, l);
        }
        if (paramInt1 < j)
          QuickSort(paramArrayOfString, paramInt1, j);
        if (i < paramInt2)
          QuickSort(paramArrayOfString, i, paramInt2);
        return;
        label130: j = l;
        i = k;
      }
      int l = j;
    }
  }

  public static void QuickSort(Ordered[] paramArrayOfOrdered)
  {
    QuickSort(paramArrayOfOrdered, 0, paramArrayOfOrdered.length - 1);
  }

  public static void QuickSort(Ordered[] paramArrayOfOrdered, int paramInt1, int paramInt2)
  {
    Ordered localOrdered;
    int k;
    int i = paramInt1;
    int j = paramInt2;
    if (paramInt2 > paramInt1)
    {
      localOrdered = paramArrayOfOrdered[((paramInt1 + paramInt2) / 2)];
      if (i <= j)
        for (k = i; (k < paramInt2) && (paramArrayOfOrdered[k].compare(localOrdered) < 0); ++k);
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while ((l > paramInt1) && (paramArrayOfOrdered[l].compare(localOrdered) > 0))
            --l;
          if (k > l)
            break label134;
          i = k + 1;
          j = l - 1;
          swap(paramArrayOfOrdered, k, l);
        }
        if (paramInt1 < j)
          QuickSort(paramArrayOfOrdered, paramInt1, j);
        if (i < paramInt2)
          QuickSort(paramArrayOfOrdered, i, paramInt2);
        return;
        label134: j = l;
        i = k;
      }
      int l = j;
    }
  }

  public static Object[] QuickSort(Hashtable paramHashtable)
    throws ClassCastException
  {
    Ordered[] arrayOfOrdered = new Ordered[paramHashtable.size()];
    Enumeration localEnumeration = paramHashtable.keys();
    int i = 1;
    for (int j = 0; j < arrayOfOrdered.length; ++j)
    {
      arrayOfOrdered[j] = localEnumeration.nextElement();
      if ((i != 0) && (!(arrayOfOrdered[j] instanceof String)))
        i = 0;
    }
    if (i != 0)
      QuickSort((String[])(String[])arrayOfOrdered);
    while (true)
    {
      return arrayOfOrdered;
      QuickSort((Ordered[])(Ordered[])arrayOfOrdered);
    }
  }

  public static int bsearch(Vector paramVector, Ordered paramOrdered)
  {
    return bsearch(paramVector, paramOrdered, 0, paramVector.size() - 1);
  }

  public static int bsearch(Vector paramVector, Ordered paramOrdered, int paramInt1, int paramInt2)
  {
    int i = -1;
    for (int j = 1 + paramInt2 - paramInt1; (-1 == i) && (paramInt1 <= paramInt2); j = k)
    {
      label11: int l;
      int i1;
      int i2;
      int k = j / 2;
      if ((j & 0x1) != 0)
        l = k;
      while (true)
      {
        while (true)
        {
          i1 = paramInt1 + l;
          i2 = paramOrdered.compare(paramVector.elementAt(i1));
          if (i2 != 0)
            break label81;
          i = i1;
        }
        l = k - 1;
      }
      if (i2 < 0)
      {
        label81: paramInt2 = i1 - 1;
        if ((j & 0x1) != 0)
          j = k;
        while (true)
        {
          break label11:
          j = k - 1;
        }
      }
      paramInt1 = i1 + 1;
    }
    if (-1 == i)
      i = paramInt1;
    return i;
  }

  public static int bsearch(Sortable paramSortable, Ordered paramOrdered)
  {
    return bsearch(paramSortable, paramOrdered, paramSortable.first(), paramSortable.last());
  }

  public static int bsearch(Sortable paramSortable, Ordered paramOrdered, int paramInt1, int paramInt2)
  {
    int i = -1;
    int j = 1 + paramInt2 - paramInt1;
    Ordered localOrdered = null;
    while ((-1 == i) && (paramInt1 <= paramInt2))
    {
      label14: int l;
      int i1;
      int i2;
      int k = j / 2;
      if ((j & 0x1) != 0)
        l = k;
      while (true)
      {
        while (true)
        {
          i1 = paramInt1 + l;
          localOrdered = paramSortable.fetch(i1, localOrdered);
          i2 = paramOrdered.compare(localOrdered);
          if (i2 != 0)
            break label92;
          i = i1;
        }
        l = k - 1;
      }
      if (i2 < 0)
      {
        label92: paramInt2 = i1 - 1;
        if ((j & 0x1) != 0)
          j = k;
        while (true)
        {
          break label14:
          j = k - 1;
        }
      }
      paramInt1 = i1 + 1;
      j = k;
    }
    if (-1 == i)
      i = paramInt1;
    return i;
  }

  public static int bsearch(Ordered[] paramArrayOfOrdered, Ordered paramOrdered)
  {
    return bsearch(paramArrayOfOrdered, paramOrdered, 0, paramArrayOfOrdered.length - 1);
  }

  public static int bsearch(Ordered[] paramArrayOfOrdered, Ordered paramOrdered, int paramInt1, int paramInt2)
  {
    int i = -1;
    for (int j = 1 + paramInt2 - paramInt1; (-1 == i) && (paramInt1 <= paramInt2); j = k)
    {
      label11: int l;
      int i1;
      int i2;
      int k = j / 2;
      if ((j & 0x1) != 0)
        l = k;
      while (true)
      {
        while (true)
        {
          i1 = paramInt1 + l;
          i2 = paramOrdered.compare(paramArrayOfOrdered[i1]);
          if (i2 != 0)
            break label79;
          i = i1;
        }
        l = k - 1;
      }
      if (i2 < 0)
      {
        label79: paramInt2 = i1 - 1;
        if ((j & 0x1) != 0)
          j = k;
        while (true)
        {
          break label11:
          j = k - 1;
        }
      }
      paramInt1 = i1 + 1;
    }
    if (-1 == i)
      i = paramInt1;
    return i;
  }

  private static void swap(Vector paramVector, int paramInt1, int paramInt2)
  {
    Object localObject = paramVector.elementAt(paramInt1);
    paramVector.setElementAt(paramVector.elementAt(paramInt2), paramInt1);
    paramVector.setElementAt(localObject, paramInt2);
  }

  private static void swap(Object[] paramArrayOfObject, int paramInt1, int paramInt2)
  {
    Object localObject = paramArrayOfObject[paramInt1];
    paramArrayOfObject[paramInt1] = paramArrayOfObject[paramInt2];
    paramArrayOfObject[paramInt2] = localObject;
  }
}